package notifications

import (
	"bytes"
	"context"
	"crypto/hmac"
	"crypto/sha256"
	"encoding/base64"
	"encoding/json"
	"fmt"
	"log"
	"net/url"
	"strconv"
	"time"
	"yundian/internel/app/helper/resphelper/placeresp"
	"yundian/internel/app/models/placemodel"
	"yundian/internel/pkg/dingo/global"
	"yundian/internel/pkg/utils"
	"yundian/internel/pkg/authorized"
)

const (
	weChatPublicTemplateSendURL         = "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=%s"
	weChatGroupNotificationsSendURL     = "https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token=%s"
	weChatGroupOfNotificationsSendURL   = "https://qyapi.weixin.qq.com/cgi-bin/appchat/send?access_token=%s"
	weChatCreateGroupChatURL            = "https://qyapi.weixin.qq.com/cgi-bin/appchat/create?access_token=%s"
	weChatGetUserId                     = "https://qyapi.weixin.qq.com/cgi-bin/user/getuserid?access_token=%s"
	dingTalkGroupNotificationsSendURL   = "https://oapi.dingtalk.com/topapi/message/corpconversation/asyncsend_v2?access_token=%s"
	dingTalkGroupOfNotificationsSendURL = "https://oapi.dingtalk.com/chat/send?access_token=%s"
	dingTalkCreateGroupChatURL          = "https://oapi.dingtalk.com/chat/create?access_token=%s"
	dingTalkGetUserId                   = "https://oapi.dingtalk.com/topapi/v2/user/getbymobile?access_token=%s"
)

//公众号个人通知
func WeChatPublicTemplateSend(bodyData map[string]interface{}, appKey, appSecret string, placesId int) (result ResSendTemplate, err error) {
	var accessToken string
	//获取授权token
	t := global.Rdb.TTL(context.Background(), utils.Join("cgi-bin-Access-Token")).Val()
	if t <= 0 {
		respAccessToken, err := authorized.GetSaveAccessToken(appKey, appSecret)
		log.Println("获取token失败", err)
		//获取不到token,则返回错误
		if err != nil {
			return result, err
		}

		accessToken = respAccessToken.AccessToken
	} else {
		s := global.Rdb.Get(context.Background(), utils.Join("cgi-bin-Access-Token")).Val()
		accessToken = s
	}

	urlStr := fmt.Sprintf(weChatPublicTemplateSendURL, accessToken)

	payload, _ := json.Marshal(bodyData)

	headers := map[string]string{"Content-Type": "application/json"}

	resp, err := authorized.PostRequest(urlStr, headers, bytes.NewReader(payload))
	body, _ := json.Marshal(resp)
	_ = json.Unmarshal(body, &result)
	if err != nil || result.Errcode != 0 {
		fmt.Printf("error: %v", err)
		key := utils.Join("cgi-bin-Access-Token")
		// 清除redis缓存中的token
		global.Rdb.Del(context.Background(), key)
		log.Println("模板消息推送失败", result.Errmsg)
		return result, err
	}

	return result, nil
}

//企业微信个人通知。文档地址：https://work.weixin.qq.com/api/doc/90000/90135/90236
func WeChatGroupNotificationsSend(bodyData map[string]interface{}, appKey, appSecret string, placesId int) (result ResSendTemplate, err error) {
	var accessToken string
	//获取授权token
	t := global.Rdb.TTL(context.Background(), utils.Join(strconv.Itoa(placesId)+"-wx-Access-Token")).Val()
	if t <= 0 {
		respAccessToken, _ := authorized.GetWeChatGroupAccessToke(appKey, appSecret, placesId)
		accessToken = respAccessToken.AccessToken
	} else {
		s := global.Rdb.Get(context.Background(), utils.Join(strconv.Itoa(placesId)+"-wx-Access-Token")).Val()
		accessToken = s
	}

	urlStr := fmt.Sprintf(weChatGroupNotificationsSendURL, accessToken)

	payload, _ := json.Marshal(bodyData)

	headers := map[string]string{"Content-Type": "application/json"}

	resp, err := authorized.PostRequest(urlStr, headers, bytes.NewReader(payload))
	body, _ := json.Marshal(resp)
	_ = json.Unmarshal(body, &result)
	if err != nil || result.Errcode != 0 {
		fmt.Printf("error: %v", err)
		key := utils.Join(strconv.Itoa(placesId) + "-wx-Access-Token")
		// 清除redis缓存中的token
		global.Rdb.Del(context.Background(), key)
		log.Println("企业微信个人通知失败", result.Errmsg)
		return result, err
	}

	return result, nil
}

//企业微信群通知。文档地址：https://work.weixin.qq.com/api/doc/90000/90135/90236
func WeChatGroupOfNotificationsSend(bodyData map[string]interface{}, appKey, appSecret string, placesId int) (result ResSendTemplate, err error) {
	var accessToken string
	//获取授权token
	t := global.Rdb.TTL(context.Background(), utils.Join(strconv.Itoa(placesId)+"-wx-Access-Token")).Val()
	if t <= 0 {
		respAccessToken, _ := authorized.GetWeChatGroupAccessToke(appKey, appSecret, placesId)
		accessToken = respAccessToken.AccessToken
	} else {
		s := global.Rdb.Get(context.Background(), utils.Join(strconv.Itoa(placesId)+"-wx-Access-Token")).Val()
		accessToken = s
	}

	urlStr := fmt.Sprintf(weChatGroupOfNotificationsSendURL, accessToken)

	payload, _ := json.Marshal(bodyData)

	headers := map[string]string{"Content-Type": "application/json"}

	resp, err := authorized.PostRequest(urlStr, headers, bytes.NewReader(payload))
	body, _ := json.Marshal(resp)
	_ = json.Unmarshal(body, &result)
	if err != nil || result.Errcode != 0 {
		fmt.Printf("error: %v", err)
		log.Println("模板消息推送失败", result.Errmsg)
		return result, err
	}

	return result, nil
}

//企业微信创建群聊会话。文档地址：https://open.work.weixin.qq.com/api/doc/90000/90135/90245
func WeChatCreateGroupChat(bodyData map[string]interface{}, appKey, appSecret string, placesId int) (result ResCreateGroupChat, err error) {
	var accessToken string
	//获取授权token
	t := global.Rdb.TTL(context.Background(), utils.Join(strconv.Itoa(placesId)+"-wx-Access-Token")).Val()
	if t <= 0 {
		respAccessToken, _ := authorized.GetWeChatGroupAccessToke(appKey, appSecret, placesId)
		accessToken = respAccessToken.AccessToken
	} else {
		s := global.Rdb.Get(context.Background(), utils.Join(strconv.Itoa(placesId)+"-wx-Access-Token")).Val()
		accessToken = s
	}

	urlStr := fmt.Sprintf(weChatCreateGroupChatURL, accessToken)

	payload, _ := json.Marshal(bodyData)

	headers := map[string]string{"Content-Type": "application/json"}

	resp, err := authorized.PostRequest(urlStr, headers, bytes.NewReader(payload))
	body, _ := json.Marshal(resp)
	_ = json.Unmarshal(body, &result)
	if err != nil || result.Errcode != 0 {
		fmt.Printf("error: %v", err)
		log.Println("企业微信创建群聊会话失败", result.Errmsg)
		return result, err
	}

	return result, nil
}

//企业微信群机器人消息通知。文档地址：https://open.work.weixin.qq.com/api/doc/90000/90136/91770
func WeChatGroupRobotNotificationsSend(bodyData map[string]interface{}, url string) (result ResSendTemplate, err error) {
	payload, _ := json.Marshal(bodyData)

	headers := map[string]string{"Content-Type": "application/json"}

	resp, err := authorized.PostRequest(url, headers, bytes.NewReader(payload))
	body, _ := json.Marshal(resp)
	_ = json.Unmarshal(body, &result)
	if err != nil || result.Errcode != 0 {
		fmt.Printf("error: %v", err)
		log.Println("群机器人通知失败", result.Errmsg)
		return result, err
	}

	return result, nil
}

//钉钉工作通知，文档地址：https://developers.dingtalk.com/document/app/asynchronous-sending-of-enterprise-session-messages
func DingTalkNotificationsSend(bodyData map[string]interface{}, appKey, appSecret string, placesId int) (result ResSendTemplate, err error) {
	resp, err := PostNotificationsSend(bodyData, strconv.Itoa(placesId)+"-ding-talk-Access-Token", appKey, appSecret, dingTalkGroupNotificationsSendURL)

	body, _ := json.Marshal(resp)
	_ = json.Unmarshal(body, &result)
	if err != nil {
		log.Println("钉钉工作通知失败", err, result.Errmsg)
		return result, err
	}

	return result, nil

}

//钉钉发送消息到企业群，文档地址：https://developers.dingtalk.com/document/app/asynchronous-sending-of-enterprise-session-messages
func DingTalkGroupOfNotificationsSend(bodyData map[string]interface{}, appKey, appSecret string, placesId int) (result DingTalkChatCreateResp, err error) {

	resp, err := PostNotificationsSend(bodyData, strconv.Itoa(placesId)+"-ding-talk-Access-Token", appKey, appSecret, dingTalkGroupOfNotificationsSendURL)

	body, _ := json.Marshal(resp)
	_ = json.Unmarshal(body, &result)
	if err != nil {
		log.Println("模板消息推送失败", err, result.Errmsg)
		return result, err
	}

	return result, nil
}

//钉钉创建群聊。文档地址：https://developers.dingtalk.com/document/app/create-group-session?spm=ding_open_doc.document.0.0.1d845f66s4nIkK#topic-1936767
func DingTalkCreateGroupChat(bodyData map[string]interface{}, appKey, appSecret string, userId int) (result DingTalkChatCreateResp, err error) {
	resp, err := PostNotificationsSend(bodyData, strconv.Itoa(userId)+"-ding-talk-Access-Token", appKey, appSecret, dingTalkCreateGroupChatURL)

	body, _ := json.Marshal(resp)
	_ = json.Unmarshal(body, &result)
	if err != nil {
		log.Println("钉钉创建群聊失败", err, result.Errmsg)
		return result, err
	}

	return result, nil
}

//钉钉群机器人消息通知。文档地址：https://developers.dingtalk.com/document/robots/custom-robot-access
func DinTalkGroupRobotNotificationsSend(bodyData map[string]interface{}, url string) (result ResSendTemplate, err error) {
	payload, _ := json.Marshal(bodyData)

	headers := map[string]string{"Content-Type": "application/json"}

	resp, err := authorized.PostRequest(url, headers, bytes.NewReader(payload))
	body, _ := json.Marshal(resp)
	_ = json.Unmarshal(body, &result)
	if err != nil || result.Errcode != 0 {
		fmt.Printf("error: %v", err)
		log.Println("群机器人通知失败", result.Errmsg)
		return result, err
	}

	return result, nil

}

//钉钉根据手机号获取userid
func DingTalkGetUserIdByMobile(phone, appKey, appSecret string, placesId int) string {
	bodyData := map[string]interface{}{
		"mobile": phone,
	}

	resp, err := PostNotificationsSend(bodyData, strconv.Itoa(placesId)+"-ding-talk-Access-Token", appKey, appSecret, dingTalkGetUserId)
	result := DingTalkGetUserId{}
	body, _ := json.Marshal(resp)
	_ = json.Unmarshal(body, &result)
	if err != nil {
		log.Println("钉钉获取userid失败", err, result.Errmsg)
		return ""
	}

	return result.DinTalkUserId.UserId
}

//企业微信根据手机号获取UserId
func WeChatGetUserIdByMobile(phone, appKey, appSecret string, placesId int) string {
	bodyData := map[string]interface{}{
		"mobile": phone,
	}

	var accessToken string
	//获取授权token
	t := global.Rdb.TTL(context.Background(), utils.Join(strconv.Itoa(placesId)+"-wx-Access-Token")).Val()
	if t <= 0 {
		respAccessToken, _ := authorized.GetWeChatGroupAccessToke(appKey, appSecret, placesId)
		accessToken = respAccessToken.AccessToken
	} else {
		s := global.Rdb.Get(context.Background(), utils.Join(strconv.Itoa(placesId)+"-wx-Access-Token")).Val()
		accessToken = s
	}

	urlStr := fmt.Sprintf(weChatGetUserId, accessToken)

	payload, _ := json.Marshal(bodyData)

	headers := map[string]string{"Content-Type": "application/json"}
	result := WeChatGetUserId{}
	resp, err := authorized.PostRequest(urlStr, headers, bytes.NewReader(payload))
	body, _ := json.Marshal(resp)
	_ = json.Unmarshal(body, &result)
	if err != nil || result.Errcode != 0 {
		fmt.Printf("error: %v", err)
		log.Println("根据手机号获取userId失败", result.Errmsg)
		return ""
	}

	return result.UserId

}

func PostNotificationsSend(bodyData map[string]interface{}, accessTokenKey, appKey, appSecret, url string) (result map[string]interface{}, err error) {
	var accessToken string
	//获取授权token
	t := global.Rdb.TTL(context.Background(), utils.Join(accessTokenKey)).Val()
	if t <= 0 {
		respAccessToken, _ := authorized.GetDingTalkAccessToken(accessTokenKey, appKey, appSecret)
		accessToken = respAccessToken.AccessToken
	} else {
		s := global.Rdb.Get(context.Background(), utils.Join(accessTokenKey)).Val()
		accessToken = s
	}

	urlStr := fmt.Sprintf(url, accessToken)

	payload, _ := json.Marshal(bodyData)

	headers := map[string]string{"Content-Type": "application/json"}

	resp, err := authorized.PostRequest(urlStr, headers, bytes.NewReader(payload))
	body, _ := json.Marshal(resp)
	_ = json.Unmarshal(body, &result)
	if err != nil || result["errmsg"] != "ok" {
		fmt.Printf("error: %v", err)
		// 清除redis缓存中的token
		global.Rdb.Del(context.Background(), accessTokenKey)
		log.Println("失败", err, result["errmsg"])
		return result, err
	}

	return result, nil

}

func DingTalkRobotUrl(secret, urlStr string) string {
	timeStampNow := time.Now().UnixNano() / 1000000
	signStr := fmt.Sprintf("%d\n%s", timeStampNow, secret)

	hash := hmac.New(sha256.New, []byte(secret))
	hash.Write([]byte(signStr))
	sum := hash.Sum(nil)

	encode := base64.StdEncoding.EncodeToString(sum)
	urlEncode := url.QueryEscape(encode)

	// 构建 请求 url
	urlStr = fmt.Sprintf("%s&timestamp=%d&sign=%s", urlStr, timeStampNow, urlEncode)

	return urlStr
}

func JointContentStr(param *placemodel.PlacesServiceNotification) (string, string) {
	infoSrt := param.Info
	noticeStr := ""
	infoMap := map[string]interface{}{}

	if len(param.ServiceInfo) > 0 && param.NotificationType < 5 && param.SendType == 2 {
		infoSrt = ""
		noticeStr = ""
		for i, v := range param.ServiceInfo {
			if i+1 != len(param.ServiceInfo) {
				noticeStr += "<font color=\"warning\">" + v + "</font>" + "、"
				infoSrt += v + "、"
			} else {
				noticeStr += "<font color=\"warning\">" + v + "</font>"
				infoSrt += v
			}
		}
	}

	if len(param.ServiceInfo) > 0 && param.NotificationType == 5 && param.InvoiceType == 1 && param.SendType <= 3 {
		infoSrt = ""
		noticeStr = ""

		for i, v := range param.ServiceInfo {
			if param.SendType == 2 {
				switch i {
				case 0:
					noticeStr += "\r\n" + "单位名称：<font color=\"warning\">" + v + "</font> " + "\r\n"
					infoMap["unitName"] = v
				case 1:
					noticeStr += "税号：<font color=\"warning\">" + v + "</font> " + "\r\n"
					infoMap["taxCode"] = v
				case 2:
					noticeStr += "金额：<font color=\"warning\">" + v + "</font> " + "\r\n"
					infoMap["money"] = v
				case 3:
					noticeStr += "开票人手机号：<font color=\"warning\">" + v + "</font> " + "\r\n"
					infoMap["phone"] = v
				}
			} else {
				switch i {
				case 0:
					noticeStr += "\n\n  >" + "单位名称：" + v + " \n\n >"
					infoMap["unitName"] = v
				case 1:
					noticeStr += "税号：" + v + " \n\n >"
					infoMap["taxCode"] = v
				case 2:
					noticeStr += "金额：" + v + " \n\n >"
					infoMap["money"] = v
				case 3:
					noticeStr += "开票人手机号：" + v + " \n\n >"
					infoMap["phone"] = v
				}
			}

		}

		infoByte, _ := json.Marshal(infoMap)
		infoSrt = string(infoByte)
	}

	if len(param.ServiceInfo) > 0 && param.NotificationType == 5 && param.InvoiceType == 2 && param.SendType <= 3 {
		infoSrt = ""
		noticeStr = ""
		for i, v := range param.ServiceInfo {
			if param.SendType == 2 {
				switch i {
				case 0:
					noticeStr += "\r\n" + "单位名称：<font color=\"warning\">" + v + "</font> " + "\r\n"
					infoMap["unitName"] = v
				case 1:
					noticeStr += "金额：<font color=\"warning\">" + v + "</font> " + "\r\n"
					infoMap["money"] = v
				case 2:
					noticeStr += "开票人手机号：<font color=\"warning\">" + v + "</font>" + "\r\n"
					infoMap["phone"] = v
				}
			} else {
				switch i {
				case 0:
					noticeStr += "\n\n  >" + "单位名称：" + v + " \n\n >"
					infoMap["unitName"] = v
				case 1:
					noticeStr += "金额：" + v + " \n\n >"
					infoMap["money"] = v
				case 2:
					noticeStr += "开票人手机号：" + v + " \n\n >"
					infoMap["phone"] = v
				}
			}

		}
		infoByte, _ := json.Marshal(infoMap)
		infoSrt = string(infoByte)
	}

	if len(param.ServiceInfo) > 0 && param.NotificationType == 6 && param.SendType <= 3 {
		infoSrt = ""
		noticeStr = ""
		for i, v := range param.ServiceInfo {

			if param.SendType == 2 {
				switch i {
				case 0:
					//noticeStr += "\r\n" + "续住日期：<font color=\"warning\">" + v + "</font> " + "\r\n"
					//infoMap["stayOverTime"] = v
					infoMap["inTime"] = v
				case 1:
					infoMap["leaveTime"] = v
					noticeStr += fmt.Sprintf(`\r\n续住：<font color="warning">%d</font> 天 \r\n`, utils.GetDayLen(param.ServiceInfo[0], v))
					//noticeStr += "订单号：<font color=\"warning\">" + v + "</font> " + "\r\n"
					//infoMap["order"] = v
				case 2:
					//noticeStr += "酒店续住类型：<font color=\"warning\">" + v + "</font> " + "\r\n"
					//infoMap["stayOverType"] = v
				}
			} else {
				switch i {
				case 0:
					noticeStr += "\n\n  >" + "续住日期：" + v + " \n\n >"
					infoMap["unitName"] = v
				case 1:
					noticeStr += "订单号：" + v + " \n\n >"
					infoMap["money"] = v
				case 2:
					noticeStr += "酒店续住类型：" + v + " \n\n >"
					infoMap["phone"] = v
				}
			}

		}

		infoByte, _ := json.Marshal(infoMap)
		infoSrt = string(infoByte)
	}

	if len(param.ServiceInfo) > 0 && param.NotificationType < 5 && param.SendType != 2 {
		infoSrt = ""
		noticeStr = ""
		for i, v := range param.ServiceInfo {
			if i+1 != len(param.ServiceInfo) {
				infoSrt += v + "、"
			} else {
				infoSrt += v
			}
		}
		noticeStr = infoSrt
	}

	if param.NotificationType == 7 {
		//infoSrt = ""
		//noticeStr = ""
		infoSrt = param.Info
		noticeStr = "退房通知"
	}

	//拼接企业开票
	if len(param.ServiceInfo) > 0 && param.NotificationType == 5 && param.InvoiceType == 1 && param.SendType > 3 {
		infoSrt = ""
		noticeStr = ""
		for i, v := range param.ServiceInfo {
			switch i {
			case 0:
				noticeStr += "\r\n" + "单位名称：" + v + " " + "\r\n"
				infoMap["unitName"] = v
			case 1:
				noticeStr += "税号：" + v + " " + "\r\n"
				infoMap["taxCode"] = v
			case 2:
				noticeStr += "金额：" + v + " " + "\r\n"
				infoMap["money"] = v
			case 3:
				noticeStr += "开票人手机号：" + v + "\r\n"
				infoMap["phone"] = v
			}
		}
		infoByte, _ := json.Marshal(infoMap)
		infoSrt = string(infoByte)
	}

	//if param.InvoiceType == 1 {
	//
	//}

	if len(param.ServiceInfo) > 0 && param.NotificationType == 5 && param.InvoiceType == 2 && param.SendType > 3 {
		infoSrt = ""
		noticeStr = ""
		for i, v := range param.ServiceInfo {
			switch i {
			case 0:
				noticeStr += "\r\n" + "单位名称：" + v + " " + "\r\n"
				infoMap["unitName"] = v
			case 1:
				noticeStr += "金额：" + v + " " + "\r\n"
				infoMap["money"] = v
			case 2:
				noticeStr += "开票人手机号：" + v + "\r\n"
				infoMap["phone"] = v
			}
		}
		infoByte, _ := json.Marshal(infoMap)
		infoSrt = string(infoByte)
	}

	//续住服务
	if len(param.ServiceInfo) > 0 && param.NotificationType == 6 && param.SendType > 3 {
		infoSrt = ""
		noticeStr = ""
		for i, v := range param.ServiceInfo {
			switch i {
			case 0:
				//noticeStr += "\r\n" + "续住日期：" + v + " " + "\r\n"
				//infoMap["stayOverTime"] = v
				infoMap["inTime"] = v
			case 1:
				//noticeStr += "订单号：" + v + " " + "\r\n"
				//infoMap["order"] = v
				infoMap["leaveTime"] = v
				noticeStr += fmt.Sprintf("续住：%d 天", utils.GetDayLen(param.ServiceInfo[0], v))
			case 2:
				//noticeStr += "酒店续住类型：" + v + " " + "\r\n"
				//infoMap["stayOverType"] = v
			}
		}
		infoByte, _ := json.Marshal(infoMap)
		infoSrt = string(infoByte)
	}

	return infoSrt, noticeStr
}

func NotificationOnDutyStaff(param []placeresp.StaffBasicInfo) []placeresp.StaffBasicInfo {
	var notificationStaff []placeresp.StaffBasicInfo

	for _, info := range param {
		var workforceMap []map[string]interface{}

		json.Unmarshal([]byte(info.WorkforceConfig), &workforceMap)
		if len(workforceMap) <= 0 {
			return param
		}
		for _, m := range workforceMap {
			//获取今天礼拜几
			weekDayStr := CalculateWeekFew(time.Now().Year(), int(time.Now().Month()), time.Now().Day())

			//时间对应判断
			if weekDayStr != m["week"] {
				continue
			}

			switch int(m["type"].(float64)) {
			case 1:
				continue
			case 2:
				notificationStaff = append(notificationStaff, info)
			case 3:
				var startHour, endHour, startMinute, endMinute, timeHour, timeMinute int
				if m["startTime"] == "" && m["endTime"] == "" {
					continue
				}

				start := m["startTime"].(string)
				end := m["endTime"].(string)
				startHour, _ = strconv.Atoi(start[0:2])
				endHour, _ = strconv.Atoi(end[0:2])
				startMinute, _ = strconv.Atoi(start[3:5])
				endMinute, _ = strconv.Atoi(end[3:5])
				timeHour = time.Now().Hour()
				timeMinute = time.Now().Minute()

				//判断是否存在时间段
				if startMinute > 0 && endMinute > 0 {
					//当开始小时等于当前小时 ，当前分钟要大于开始的分钟
					if startHour == timeHour && startMinute <= timeMinute {
						notificationStaff = append(notificationStaff, info)
					}

					if endHour == timeHour && endMinute <= timeMinute {
						notificationStaff = append(notificationStaff, info)
					}

					if startHour <= timeHour && endHour >= timeHour {
						notificationStaff = append(notificationStaff, info)
					}
				} else {
					if startHour <= timeHour && endHour >= timeHour {
						notificationStaff = append(notificationStaff, info)
					}
				}

			}

		}

	}

	return notificationStaff
}

var WeekDay = [7]string{"星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六"}

func CalculateWeekFew(year, month, day int) string {
	var y, m, c int
	if month >= 3 {
		m = month
		y = year % 100
		c = year / 100
	} else {
		m = month + 12
		y = (year - 1) % 100
		c = (year - 1) / 100
	}

	week := y + (y / 4) + (c / 4) - 2*c + ((26 * (m + 1)) / 10) + day - 1
	if week < 0 {
		week = 7 - (-week)%7
	} else {
		week = week % 7
	}
	whichWeek := week
	return WeekDay[whichWeek]
}
